package com.webserver.controller;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;
import com.webserver.vo.User;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ArticleController {
    private static String article="./article/";
    static {
        File file=new File(article);
        if(file.isDirectory()){
            file.mkdirs();
        }
    }
    public void writeArticle(HttpRequest request,HttpResponse response){

        String q="./webapps/myweb/";
        String title=request.getParameter("title");
        String content=request.getParameter("content");
        String author=request.getParameter("author");
        if(title==null||content==null){
            response.setEntity(new File(q+"article_fail.html"));
            return;
        }
        File file=new File(article+title+".obj");
        try(
                ObjectOutputStream oos=new ObjectOutputStream(
                        new FileOutputStream(file));
                ) {
            Article article1=new Article(title,content,author);
            oos.writeObject(article1);
        response.setEntity(new File(q+"article_success.html"));
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    public void showAllArticle(HttpRequest request,HttpResponse response){
        System.out.println("开始生成动态页面");
        List<Article>arrayList=new ArrayList<>();
        File file=new File(article);
        File[]arr=file.listFiles(f->f.getName().endsWith(".obj"));
        for(File file1:arr){
            try(
                    ObjectInputStream obj=new ObjectInputStream(
                            new FileInputStream(file1)
                    )
                    ){
                arrayList.add((Article) obj.readObject());
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        arrayList.forEach(System.out::println);
        PrintWriter p=response.getWriter();
        p.println("<!DOCTYPE html>");
        p.println("<html lang=\"en\">\n");
        p.println("<head>\n");
        p.println("    <meta charset=\"UTF-8\">\n");
        p.println("    <title>文章列表</title>\n");
        p.println("</head>\n");
        p.println("<body>\n");
        p.println("<center>\n");
        p.println("    <H1>文章列表</H1>\n");
        p.println("        <table border=\"1\">\n");
        p.println("            <tr>\n");
        p.println("                <td>用户名</td>\n");
        p.println("                <td>密码</td>\n");
        p.println("                <td>昵称</td>\n");
        p.println("            </tr>\n");
        for(Article article :arrayList){
            p.println("            <tr>\n");
            p.println("<td>"+article.getTitle()+"</td>");
            p.println("<td>"+article.getAuthor()+"</td>");
            p.println("<td>"+article.getContent()+"</rd>");
            p.println("            </tr>\n");

        }
        p.println("        </table>\n");
        p.println("</center>\n");
        p.println("</body>\n");
        p.println("</html>");
        response.setContentType("text.html");
        System.out.println("动态页面生成结束");
    }
}
